#include "musicinfo.h"

MusicInfo::MusicInfo()
    :isLike(false),
     isHistory(false)
{
}

MusicInfo::MusicInfo(const QUrl &url)
    :isLike(false),
     isHistory(false),
     musicUrl(url)
{
    //创建一个通用的UUid
    musicId = QUuid::createUuid().toString();   //唯一标识符
    //需要在初始化阶段提前获取到对应的数据，以便后续的使用
    praseMediaMetaData();
}

void MusicInfo::setIsLike(bool isLike)
{
    this->isLike = isLike;
}

void MusicInfo::setIsHistory(bool isHistory)
{
    this->isHistory = isHistory;
}

void MusicInfo::setMusicName(const QString &musicName)
{
    this->musicName = musicName;
}

void MusicInfo::setSingerName(const QString &singerName)
{
    this->singerName = singerName;
}

void MusicInfo::setAlbumName(const QString &albumName)
{
    this->albumName = albumName;
}

void MusicInfo::setDuration(const qint64 duration)
{
    this->duration = duration;
}

void MusicInfo::setMusicUrl(const QUrl &url)
{
    this->musicUrl = url;
}

void MusicInfo::setMusicId(const QString &musicId)
{
    this->musicId = musicId;
}

bool MusicInfo::getIsLike()
{
    return this->isLike;
}

bool MusicInfo::getIsHistory()
{
    return isHistory;
}

QString MusicInfo::getMusicName()
{
    return musicName;
}

QString MusicInfo::getSingerName()
{
    return singerName;
}

QString MusicInfo::getAlbumName()
{
    return albumName;
}

qint64 MusicInfo::getDuration()
{
    return duration;
}

QUrl MusicInfo::getMusicUrl()
{
    return musicUrl;
}

QString MusicInfo::getMusicId()
{
    return musicId;
}

QString MusicInfo::getLrcFilePath()
{
    QString path = musicUrl.toLocalFile();          //找到对应的路径
    //把本地的路径替换成.lrc
    path.replace(".mp3",".lrc");
    path.replace(".flac",".lrc");
    path.replace(".mpga",".lrc");
    return path;
}

void MusicInfo::praseMediaMetaData()
{
    //1.创建解析类
    QMediaPlayer mediaPlayer;
    mediaPlayer.setMedia(musicUrl);
    //2.等待解析完成，中间要保证主进程任然继续接收处理数据
    while(!mediaPlayer.isMetaDataAvailable())
    {
        QCoreApplication::processEvents();
    }
    //3.解析完成，填充对应的数据到对应的字段中
    if(mediaPlayer.isMetaDataAvailable())
    {
        musicName = mediaPlayer.metaData("Title").toString();
        singerName = mediaPlayer.metaData("Author").toString();
        albumName = mediaPlayer.metaData("AlbumTitle").toString();
        duration = mediaPlayer.duration();

        QString fileName = musicUrl.fileName();
        int index = fileName.indexOf('-');

        //如果没有对应的数据就设置为未知
        if(musicName.isEmpty())
        {
            if(index != -1)
            {
                // "2002年的第⼀场雪 - ⼑郎.mp3"
                //trimmed用来去除前后的空格
                musicName = fileName.mid(0,index).trimmed();
            }
            else
            {
                // "2002年的第⼀场雪.mp3"
                musicName = fileName.mid(0,fileName.indexOf('.')).trimmed();
            }
        }
        if(singerName.isEmpty())
        {
            if(index != -1)
            {
                singerName = fileName.mid(index+1,fileName.indexOf('-')).trimmed();
            }
        }
        if(albumName.isEmpty())
        {
            albumName = "未知专辑";
        }
        //打印查看信息是否正确
        qDebug() << musicName << singerName << albumName << duration;
    }
}
